<?php
class detailAction extends CommonAction {
	/*
	  status 判断类型
	  0.待审核1.已确认2.已取消3.已发放
	*/
	public function index(){
	   $this->dao = D('member_account_log');
	   if($this->_get('stime') || $this->_get('etime')){
	      $this->stime = $this->_get('stime');
		  $this->etime = $this->_get('etime');
		  $stime = strtotime($this->_get('stime'));
		  $etime = strtotime($this->_get('etime'))+24*3600;
		 if($this->_get('stime')){
		  $this->listmap.=" and pdstate=2 and dateline >= $stime ";
		 }
		 if($this->_get('etime')){
		  $this->listmap.=" and pdstate=2 and dateline <= $etime ";
		 }
		 if($this->_get('etime') && $this->_get('stime')){
		  $this->listmap.=" and pdstate=2 and dateline >= $stime and dateline <= $etime ";
		 }
	   }else{
	     $this->listmap.=" and pdstate=2";
	   }
	   if($this->_get('status','int') || $this->_get('status') == '0'){
			$this->listmap .= " and status = ".$this->_get('status','int');
			$this->status = $this->_get('status');
		}
	   if($this->_get('hbstatus','int') || $this->_get('hbstatus') == '0'){
			$this->listmap .= " and hbstatus = ".$this->_get('hbstatus','int');
			$this->hbstatus = $this->_get('hbstatus');
		}
	   if($this->_get('getExcel') > 0){
		   $this -> getExcel();
		   exit;
	   }
       //$this->listmap.=" and pdstate=2";
       parent::_list();
	   //$member_account_logDB = D('member_account_log');
	   //$data = $member_account_logDB->where(array('pdstate'=>2))->select();
	   //$this->assign('list',$data);
	   $this->display();
	}
	//确认提现
	public function is_detail(){
	  $id = $this->_get("id");
	  $member_account_logDB = M('member_account_log'); 
	  $data = $member_account_logDB->where(array('id'=>$id))->find();
	  if($data['status'] == 1){
		  $this->error('已经确认');
	  }else{
	    $member_account_logDB->where(array('id'=>$id))->save(array('status'=>1));
		
		$wxuser=M('Member')->where('id = '.$data['member_id'])->find();	
		$text = "您的提现审核已经通过，实际到账时间以银行信息为准！";
			
		sendWXMsg($wxuser['openid'], $text);
		
		
		$this->success('提现审核成功');
	  }
	}
	//取消提现
	public function exit_detail(){
	  $id = $this->_get("id");
	  $member_account_logDB = D('Member_account_log');
	  $data = $member_account_logDB->where(array('id'=>$id, 'status'=>array('neq',2), 'pdstate' => 2))->find();
	  if(!$data){
		  $this->error('非法操作');
	  }else{
		  if($data['hbstatus']==1){
		$ret = $member_account_logDB -> change($data['goodsMoney'], $data['member_id'], '取消提现，提现日志为'.$data['id']);  
		  }
		  if($data['hbstatus']==2){
		$ret = $member_account_logDB -> change($data['goodsMoney'], $data['member_id'], '取消提现，提现日志为'.$data['id'],3);
		  }
		if($ret){
			$member_account_logDB->where(array('id'=>$id))->save(array('status'=>2));
			$this->success('取消提现成功');
		}else{
			$this->error('取消失败');
		}
	  }
	}
	
	//查看提现详情
	public function find_detail(){
	   $id = $this->_get("id");
	   $member_account_logDB = M('member_account_log');
	   $data = $member_account_logDB->where(array('id'=>$id))->find();
	   $mem_brankid = $data['mem_brankid'];
	   
	   $mem_brank = M('member_bank')->where(array('id'=>$mem_brankid))->find();
	   $this->assign('brank',$mem_brank);
	   $this->display();
	}
	
	//
	public function add(){
	   
	   $mes = M('site')->where(array('id'=>$this->siteid))->find();
	   $this->assign(unserialize($mes['bonusconfig']));
	   $this->display();
	}
	
	//设置
	public function _upload($file = 'file') {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile(); 
        //设置上传文件大小
        $upload->maxSize = 1*1024*1024;
        $upload->allowExts = array('pem');
        $upload->savePath = "./Public/Uploads/".$this->siteid.'/'. MODULE_NAME . "" . $this->siteid . "/";
        //自动命名
        //$upload->saveRule = 'uniqid';
		
       
        if (!file_exists("$upload->savePath")) {
            mk_dir($upload->savePath);
        }
        
        $upload->uploadReplace = true;
        if (!$upload->upload()) {
            $error = $upload->getErrorMsg();
            return false;
        } else {
            $uploadSuccess = true;
            $uploadList = $upload->getUploadFileInfo();
			
            return $uploadList;
        }
    }
	
	
	public function setup(){
	    
	    $poundage = $this->_post('poundage');
		$mindetail = $this->_post('mindetail');
		$files = $this->_upload();
		//if($files){
		$i = 0;
		
		if($_FILES['apiclient_cert']['name'] == ''){
			unset($_FILES['apiclient_cert']);
		}
		if($_FILES['apiclient_key']['name'] == ''){
			unset($_FILES['apiclient_key']);
		}
		foreach($_FILES as $k => $v){
			//if($files[$i]['savepath']. $files[$i]['savename']){
				$_POST[$k] = $files[$i]['savepath']. $files[$i]['savename'];
			//}
			
			$i ++;
		}
		//}
		
		
		$sdb = M('Site');
		$mes = $sdb->where(array('id'=>$this->siteid))->save(array('poundage'=>$poundage,'mindetail'=>$mindetail,'bonusconfig' => serialize($_POST)));
		
		
		if($mes !== false){
		   $this->success('设置成功');	
		}else{
		   $this->error('设置失败');
		}
	  
	}
	
	//红包提现
	public function hongbao(){
		
		$id = $this->_get('id');
	    $member_account_logDB = M('member_account_log');
		$mal = $member_account_logDB->where(array('id'=>$id ,'status'=>3))->find();
		if($mal){ 
			$this->error('请不要重复操作');exit;
		}
		$memberDB = M('member');
		$mes = $member_account_logDB->where(array('id'=>$id))->find();
		$changenum = $mes['changenum'];
		$member = $memberDB->where(array('id'=>$mes['member_id']))->find();
		$result = sendBonus($member['openid'],$mes['changenum'],$this->siteid);
		$mes = false;
		if($result -> return_code == 'SUCCESS' && $result -> result_code == 'SUCCESS'){
			$mes = $member_account_logDB->where(array('id'=>$id))->save(array('status'=>3));
		}else{
			$this->error('提现失败:'.$result -> return_msg);exit;
		}
		if($mes){
			sendWXMsg($member['openid'], '您申请提现成功，到款金额：¥'.$changenum.'');
		  $this->success('提现成功');
		}else{
		   $this->error('提现失败');
		}
	    //dump(sendBonus('oSWvqs40qN8fH4TJ8pDmLVdoaKkM','1','13')); 

	}
	
	//导出Excel
	public function getExcel(){
	
	   Vendor('Excel.PHPExcel');
	   $excel = new PHPExcel();
	   
	   set_time_limit (0);
	   
	  //取出数据
	  $member_account_logDB = D('member_account_log');
	  $member_bankDB = M('member_bank');
	  $data = $member_account_logDB->where($this->listmap)->select();
	  
	  foreach($data as $key=>$value){
	      
		  if($data[$key]['status'] == 0){
			  
		     $data[$key]['status'] = "待确认";
			 
		  }elseif($data[$key]['status'] == 1){
			  
			 $data[$key]['status'] = "确认";
			  
		  }elseif($data[$key]['status'] == 2){
			  
			  $data[$key]['status'] = "已取消";
			  
		  }elseif($data[$key]['status'] == 3){
			  
			  $data[$key]['status'] = "已完成";
			  
		  }
		  
		  
		  
		  if($data[$key]['hbstatus'] == 0){
			  
		     $data[$key]['hbstatus'] = "红包提现";
			 
		  }elseif($data[$key]['hbstatus'] == 1){
			  
			 $data[$key]['hbstatus'] = "大额提现";
			  
		  }
		  
		  
		   $data[$key]['dateline'] = date("Y-m-d",$data[$key]['dateline']);
		   
		    $bank = $member_bankDB->where(array('bank_id'=>$data[$key]['mem_brankid']))->find();
	        $data[$key]['depositbank'] = $bank['depositbank'];
			$data[$key]['accountname'] = $bank['accountname'];
			$data[$key]['account'] = $bank['account'];
			$data[$key]['accountzone'] = $bank['accountzone'];
			$data[$key]['bankname'] = $bank['bankname'];
	  
	  }
	   
	  $letter = array('A','B','C','D','E','F','G','H','I','J','K');
	  $tableheader = array('ID','提现说明','金额','状态','提现类型','申请时间','开户行','账户名','账号','开户行地区','支行名称');
	
	  $excel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
	  $excel->getActiveSheet()->getColumnDimension('B')->setWidth(66);
	  $excel->getActiveSheet()->getColumnDimension('C')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('D')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('E')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('F')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('G')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('H')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('I')->setWidth(32);
	   $excel->getActiveSheet()->getColumnDimension('J')->setWidth(32);
	  $excel->getActiveSheet()->getColumnDimension('K')->setWidth(32);
	
	  for($i = 0;$i < count($tableheader);$i++) {
		  
	   $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
	  
	  }
	  
	  
	
	  for ($i = 2; $i <= count($data)+1; $i++) {
		  
		 $excel->getActiveSheet()->setCellValue('A'.$i, $data[$i-2]['id']);
		 $excel->getActiveSheet()->setCellValue('B'.$i, $data[$i-2]['remark']);
		 $excel->getActiveSheet()->setCellValue('C'.$i, $data[$i-2]['changenum']);
		 $excel->getActiveSheet()->setCellValue('D'.$i, $data[$i-2]['status']);
		 $excel->getActiveSheet()->setCellValue('E'.$i, $data[$i-2]['hbstatus']);
		 $excel->getActiveSheet()->setCellValue('F'.$i, $data[$i-2]['dateline']);
		 $excel->getActiveSheet()->setCellValue('G'.$i, $data[$i-2]['depositbank']);
		 $excel->getActiveSheet()->setCellValue('H'.$i, $data[$i-2]['accountname']);
		 $excel->getActiveSheet()->setCellValue('I'.$i, ' '.$data[$i-2]['account']);
		 $excel->getActiveSheet()->setCellValue('J'.$i, $data[$i-2]['accountzone']);
		 $excel->getActiveSheet()->setCellValue('K'.$i, $data[$i-2]['bankname']);
	}
	
	
		
		$write = new PHPExcel_Writer_Excel5($excel);
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");;
		header('Content-Disposition:attachment;filename="提现明细.xls"');
		header("Content-Transfer-Encoding:binary");
		$write->save('php://output');
	
	exit;
	
	
	
	}

     	
}
